/*
 * motomain.c
 *
 *  Created on: Mar 21, 2012
 *      Author: Eug
 */

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/iom48.h>
#include <stdio.h>
#include "biwire.h"

#define PW1 1
#define PW2 2

void hw_init()
{
	// Ports
	// enable PB1 as power LED
	DDRB = 0xff;

	// PIN1 PORTB set -> LED on
	PORTB = 0xff;

}

volatile uint8_t pwm1, pwm2, pcnt;

void control_pwm(void)
{
	if(++pcnt>5) {
		pcnt=0;
		PORTB |= _BV(PW2) | _BV(PW1);
	}

	if (pcnt>=pwm1) {
		PORTB &= ~_BV(PW1);
	}
	if (pcnt>=pwm2) {
		PORTB &= ~_BV(PW2);
	}

}

int main(void)
{
	hw_init();
	biwire_init();
	sei();

	while (1) {
		if ( BwButton & BW_EVENT ) {
			BwButton &= ~BW_EVENT; // Clear event flag
#if 0
			// Test
			if (BwButton & BW_EVLNG) {
				if (BwButton & BW_EVBT1) {
					pwm1 = 0;
				} else if (BwButton & BW_EVBT2) {
					pwm2 = 0;
				}
				BwIndicate = OrangeRG;
				BwLedDelay = 5;
			} else if (BwButton & BW_EVOPN) {
				//BwIndicate = LedOff;
			} else if (BwButton & BW_EVBT1) {
				if(++pwm1 > 4) pwm1 = 0;
				BwIndicate = OneRed;
				BwLedDelay = 5;
			} else if (BwButton & BW_EVBT2) {
				if(++pwm2 > 4) pwm2 = 0;
				BwIndicate = OneGreen;
				BwLedDelay = 5;
			}
#else
			if (BwButton & BW_EVBT1) {
				if(++pwm1 > 4) pwm1 = 0;
				BwIndicate = OneRed;
				BwLedDelay = 5;
			} else if (BwButton & BW_EVBT2) {
				if(++pwm2 > 4) pwm2 = 0;
				BwIndicate = OneGreen;
				BwLedDelay = 5;
			}
#endif
		}
	}
}
